/* debug.c

Copyright (C) Alex S. (mailto: md5sum@alexsp.ru)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details
http://www.gnu.org/licenses/gpl-2.0.html.

You should have received a copy of the GNU General Public License
along with this program in the file gpl-2.0.txt;
if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "hardware.h"
#include <string.h>
#include "m41t81.h"

#define USART_BAUD 38400UL

char debug_str[20];
#define out_str debug_str

char usart_rx_buff[20];
uint8_t rx_str_flag=0;
uint8_t rx_len=0;

typedef enum{Wait,I,M,Row,Col,RX,B,BgL,C,L,G,E,GET} rx_state_t;

void init_usart(void) __attribute__ ((naked)) __attribute__ ((section (".init6")));
void init_usart(void){
	/*USART для диагностики*/
	DDRD|=(1<<PD1);
	UCSRA=(1<<U2X);
	UCSRB=(1<<RXCIE)|(0<<TXCIE)|(0<<UDRIE)|(1<<RXEN)|(1<<TXEN);
	UCSRC=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);
	UBRRL=(F_CPU/(8*USART_BAUD))-1; //51- 9600
	UBRRH=0;
}

void debug(const char * str){
	char * p=(char *)str;

	while (*p) {
#if USE_Watchdog==1
		wdt_reset();
#endif /* USE_Watchdog */
		while (!(UCSRA&(1<<UDRE)));
		UDR=*p;
		p++;
	}
}

ISR(USART_RXC_vect){
	uint8_t tmp;

	tmp=UDR;
	UDR=tmp;
	if (rx_str_flag) return;

	usart_rx_buff[rx_len]=tmp;
	if (tmp=='\r') {
		usart_rx_buff[rx_len]=0;
		rx_str_flag=1;
	}
	rx_len++;
	if (rx_len>=sizeof(usart_rx_buff)) rx_len=0;
}

void parse_cmd(){
	if (rx_str_flag) {
		/*
		debug("read: ");
		debug(usart_rx_buff);*/
		debug("\r\n");

		if (strlen(usart_rx_buff)==0) {
			debug("(1)Date time:\t");
			itoa(time.year+2000,&debug_str[0],10);
			itoa(time.month/10,&debug_str[4],10);
			itoa(time.month%10,&debug_str[5],10);
			itoa(time.day/10,&debug_str[6],10);
			itoa(time.day%10,&debug_str[7],10);
			debug_str[8]=' ';
			itoa(time.hour/10,&debug_str[9],10);
			itoa(time.hour%10,&debug_str[10],10);
			itoa(time.min/10,&debug_str[11],10);
			itoa(time.min%10,&debug_str[12],10);
			itoa(time.sec/10,&debug_str[13],10);
			itoa(time.sec%10,&debug_str[14],10);
			debug(debug_str);
			debug("\r\n");

			debug("(2)Time adj(+-31):\t");
			itoa(m41_control,debug_str,10);
			debug(debug_str);
			debug("\r\n");

			debug("(3)P adj(+-999):\t");
			itoa(hop03_correct,debug_str,10);
			debug(debug_str);
			debug("\r\n");

			debug("(4)Led max(1-24):\t");
			itoa(led_power_max,debug_str,10);
			debug(debug_str);
			debug("\r\n");

			debug("(5)Led min(0-23):\t");
			itoa(led_power_min,debug_str,10);
			debug(debug_str);
			debug("\r\n");

			debug("(6)Led sec(0-255):\t");
			itoa(led_time,debug_str,10);
			debug(debug_str);
			debug("\r\n");
			debug("Enter num,value (as present, with leader zero!)");
			debug("\r\n");
			debug("\r\n");
		}else if (strstr(usart_rx_buff,"1,")==usart_rx_buff){
			m41stop();
			time.year=(usart_rx_buff[4]-'0')*10+(usart_rx_buff[5]-'0');
			time.month=(usart_rx_buff[6]-'0')*10+(usart_rx_buff[7]-'0');
			time.day=(usart_rx_buff[8]-'0')*10+(usart_rx_buff[9]-'0');
			time.hour=(usart_rx_buff[11]-'0')*10+(usart_rx_buff[12]-'0');
			time.min=(usart_rx_buff[13]-'0')*10+(usart_rx_buff[14]-'0');
			time.sec=0;
			m41set_time();
		}else if (strstr(usart_rx_buff,"2,")==usart_rx_buff){
			m41_control=(usart_rx_buff[2]-'0')*10+(usart_rx_buff[3]-'0')*10+(usart_rx_buff[4]-'0');
			m41calibrate();
		}else if (strstr(usart_rx_buff,"3,")==usart_rx_buff){
			hop03_correct=(usart_rx_buff[2]-'0')*100+(usart_rx_buff[3]-'0');
			eep_save_set();
		}else if (strstr(usart_rx_buff,"4,")==usart_rx_buff){
			led_power_max=(usart_rx_buff[2]-'0')*10+(usart_rx_buff[3]-'0');
			lcd_backlight_on(led_power_max);
			led_time_left=led_time;
			eep_save_set();
		}else if (strstr(usart_rx_buff,"5,")==usart_rx_buff){
			led_power_min=(usart_rx_buff[2]-'0')*10+(usart_rx_buff[3]-'0');
			lcd_backlight_on(led_power_min);
			led_time_left=0;
			eep_save_set();
		}else if (strstr(usart_rx_buff,"6,")==usart_rx_buff){
			led_time=(usart_rx_buff[2]-'0')*100+(usart_rx_buff[3]-'0')*10+(usart_rx_buff[4]-'0');
			led_time_left=led_time;
			eep_save_set();
		}

		rx_str_flag=0;
		rx_len=0;
	}
}
